import com.aliasi.lm.TrieCharSeqCounter
import com.inamik.utils.TableFormatter
import com.inamik.utils.TableFormatter as TF
import com.inamik.utils.SimpleTableFormatter
import com.google.common.io.Resources
import com.google.common.collect.Maps

class PrintNgrams {
    static main(String[] args) {
        String input = args[0]
        try{
            def resUrl = Resources.getResource(args[0])
            def file = new File(resUrl.toURI())
            input = file.readLines('utf-8').join(' ')
        } catch(IllegalArgumentException) {}

        TrieCharSeqCounter counter = new TrieCharSeqCounter(8)
        counter.incrementSubstrings(input)
        TableFormatter tf = new SimpleTableFormatter(true)

        tf.nextRow().nextCell().addLine("count")
        tf.nextCell().addLine("ngrams")
        Map<Integer, Set<String>> countNgramMap = [:]
        counter.topNGrams(4, 16).each { ngram, count ->
            println "found $count"
            if(countNgramMap[count] == null) print "Entry for $count NULL"
            if(!countNgramMap[count]) countNgramMap.put(count, [ngram].toSet())
            println "map entry: ${countNgramMap[count]}"
            countNgramMap[count].add(ngram)
        }

        countNgramMap.each { count, ngrams -> tf.nextRow()
            tf.nextCell(TF.ALIGN_RIGHT, TF.VALIGN_TOP).addLine(count.toString())
            def ngramLine = ngrams.inject([]) { line, ngram -> line << "'$ngram'" }.join(' ')
            tf.nextCell().addLine(ngramLine)

        }
        tf.formattedTable.each {println it}
    }
}
